package com.product.magina.webserviceclient.wsClient;


import com.alibaba.fastjson.JSON;
import com.product.magina.webserviceclient.service.impl.DemoModel;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


/**
 *  CXF_webService基本用法
 *  @author：anti_magina
 *  @date：2019-03-29 16:49:26
 */
public class WS_CXF_BasicClient {

    public static void main(String[] args) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8081/services/TestService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            DemoModel model = new DemoModel();
            model.setId("id000001");
            model.setUserName("I am id000001!");
            String json = JSON.toJSONString(model);
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("saveMessage", json);

            // invoke("方法名",参数1,参数2,参数3....);
//            objects = client.invoke("sendMessageByUsername", "Magina");
//            objects = client.invoke("sendMessageById", "<我是Id>","<我叫Magina>");
            System.out.println("返回数据:" + objects[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
